<?php

/**
 * NOTICE OF LICENSE
 *
 * THIS SOURCE FILE IS PART OF EVEBIT'S PRIVATE PROJECT.
 * 
 * DO NOT USE THIS FILE IN OTHER PLACE.
 *
 * @category  Evebit
 * @package   App_Model
 * @author    Cheng Wei <berlios.o@gmail.com>
 * @version   $$Id: Adapter.php 130 2011-03-18 03:10:02Z cheng.wei $$
 * @copyright Copyright (c) 2008 Evebit Inc. China (http://www.evebit.com)
 */

/**
 * Paginator
 * 
 * @package App_Model
 * @author  Cheng Wei <berlios.o@gmail.com>
 * @version $$Id: Adapter.php 130 2011-03-18 03:10:02Z cheng.wei $$
 */
class Evebit_Model_Paginator_Adapter implements Zend_Paginator_Adapter_Interface {
	
	/**
	 * @var Evebit_Model_Mysql_Collection
	 */
	protected $collection = null;
	
	/**
	 * Construct
	 * 
	 * @param Evebit_Model_Mysql_Collection $collection
	 */
	public function __construct($collection = null) {
		$this->setCollection($collection);
	}
	
	/**
	 * @param Evebit_Model_Mysql_Collection $collection the $collection to set
	 * @return Evebit_Model_Paginator_Adapter
	 */
	public function setCollection($collection) {
		$this->collection = $collection;
		return $this;
	}
	
	/**
	 * @return Evebit_Model_Mysql_Collection the $collection
	 */
	public function getCollection() {
		return $this->collection;
	}
	
	/**
	 * get items
	 * 
	 * @param int $offset
	 * @param int $itemCountPerPage
	 * 
	 * @return array
	 */
	public function getItems($offset, $itemCountPerPage) {
		$collection = $this->getCollection();
		if (!$collection) {
			return null;
		}
		if ($itemCountPerPage) {			
			$collection->setPageSize($itemCountPerPage);
			$collection->setCurPage(intval($offset / $itemCountPerPage)+1);
		}
		return $collection->getItems();
	}
	
	/**
	 * @return int
	 */
	public function count() {
		$collection = $this->getCollection();
		if (!$collection) {
			return 0;
		}
		return $collection->getSize();
	}

}